import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.linear_model import RidgeCV
from sklearn.preprocessing import StandardScaler
from sklearn.feature_selection import SelectFromModel
import scipy.stats as stats
from scipy.stats import zscore

import matplotlib as mpl

mpl.use('TkAgg')

X = pd.read_csv("data/factor_pred.csv", index_col=0)
X = X.drop(["Year","Month","Day"],axis=1)
y = pd.read_csv("data/PM.csv")
# X = X.drop(["V10004_700", "V12001_700", "V13003_700"], axis=1)
#
columnName_List = []
for (columnName, columnData) in X.items():
    print('Colunm Name : ', columnName)
    columnName_List.append(columnName)

# scaler = StandardScaler()
# X = scaler.fit_transform(X)
# print(X)
# print(y)
#
print(columnName_List)
ridge = RidgeCV(alphas=np.logspace(-6, 6, num=5)).fit(X, y)
importance = np.abs(ridge.coef_)
importance = importance[:][0]
print(importance)
#
feature_names = np.array(columnName_List)
print(feature_names)
plt.figure(figsize=(16, 9), dpi=300)
plt.bar(height=importance, x=feature_names)
plt.title("Feature importances via coefficients")
plt.savefig("img/demo.png")

threshold = np.sort(importance)[-3] + 0.01

sfm = SelectFromModel(ridge, threshold=threshold).fit(X, y)
print(f"Features selected by SelectFromModel: {feature_names[sfm.get_support()]}")


# 假设性检验
# 定义一个函数来检验每一列数据是否服从正态分布
def ks_test(column):
    # 使用scipy.stats.kstest函数进行检验，指定cdf为norm
    u = column.mean()
    std = column.std()
    result = stats.kstest(column, "norm", (u, std))
    print(result)
    # 获取统计量和p值
    statistic = result.statistic
    p_value = result.pvalue
    # 定义一个空列表来存储显著性水平和检验结果
    levels = []
    outcomes = []
    # 遍历不同的显著性水平（15%, 10%, 5%, 2.5%, 1%）
    for level in [0.15, 0.1, 0.05, 0.025, 0.01]:
        # 如果p值大于显著性水平，则不能拒绝原假设，即样本数据符合正态分布
        if p_value > level:
            outcome = "Normal"
        # 否则拒绝原假设，即样本数据不符合正态分布
        else:
            outcome = "Not normal"
        # 将显著性水平和检验结果添加到对应的列表中
        levels.append(level * 100)
        outcomes.append(outcome)
    # 返回一个包含统计量、p值、显著性水平和检验结果的数据框
    return pd.DataFrame(
        {"Statistic": statistic, "P-value": p_value, "Significance level (%)": levels, "Outcome": outcomes})


if __name__ == "__main__":
    # 读取csv文件中的数据
    data = pd.read_csv("data/factor.csv",index_col=0)
    print(data)
    column_list = []
    for (columnName, columnData) in data.items():
        column_list.append(columnName)
        data[columnName] = zscore(data[columnName])

    print(data)

    # 遍历数据中的每一列，打印列名和检验结果
    for column in data.columns:
        print(column)
        print(data[column])
        print(ks_test(data[column]))
